
#include "../MyWindow.h"
#include "../Camera.h"

#include "../leak.h"

#include "globals.h"

extern float rot_vec[3];
extern float move_vec[3];

class ViewModelWindow : public MyBaseWindow {

	MyGLView main_viewport_;
	cSlot< ViewModelWindow, int > button_pressed_slot_;

	bool cam_rot_enabled_;

public:
	//MyMainWindow(HWND window_handle, char flags ) : MyBaseWindow(window_handle, flags), cam_rot_enabled_(false){
	ViewModelWindow();

	void SetupMainViewPort ( Camera& cam ) {

		const int* v = main_viewport_.dims_px();
		
		cam.Perspective( 45.0f, (float)v[2]/v[3], 0.1f, 10000.0f );

		main_viewport_.SetActive();

	}

	virtual int OnClick( float x, float y) {
		MyBaseWindow::OnClick( x, y );

		if( main_viewport_.Check( x, y ) )
		{
			//listen_mouse_move = true;
			cam_rot_enabled_ = true;

			extern bool view_update_needed;
			view_update_needed = true;
			//std::cout << "rotate!!! : " << x << " " << y << std::endl;
		} else
		{
			extern bool view_update_needed;
			view_update_needed = false;
		}

		return 0;
	}

	virtual int OnMove( float dx, float dy ) {
		
		if( MyBaseWindow::OnMove( dx, dy ) == -1 )
		{
			return -1;
		}

		if( update_needed_ )
		{

		main_viewport_.Resize( -1.0f + 17.0f/window_size_[0],
								-1.0f + 0.1f + 17.0f/window_size_[1],
								2.0f - 32.0f/window_size_[0], 
								2.0f - 0.1f - 160.0f/window_size_[1],
								window_size_ );

		}

		update_needed_ = false;

		if( cam_rot_enabled_ )
		{
			
			rot_vec[0] = dy * 100.0f;
			rot_vec[1] = dx * 100.0f;

			g_render_state.current_cam->Rotate( rot_vec, 1.0f );

		}

		return 0;
	}

	virtual int OnRelease( float x, float y) {
		MyBaseWindow::OnRelease( x, y );

		rot_vec[0] = 0.0f;
		rot_vec[1] = 0.0f;

		cam_rot_enabled_ = false;

		return 0;
	}



	virtual int OnKeyPress( std::vector< unsigned char >& keys ) {

		//if( !keys.empty() )
		//std::cout << "first_key: " << keys[0] << std::endl;

		move_vec[0] = 0.0f;
		move_vec[1] = 0.0f;
		move_vec[2] = 0.0f;

		for(auto key : keys)
		switch (key)
		{
		case 'w':
			move_vec[2] = -0.2f;
			break;
		case 's':
			move_vec[2] = 0.2f;
			break;
		case 'a':
			move_vec[0] = -0.2f;
			break;
		case 'd':
			move_vec[0] = 0.2f;
			break;
		case SDLK_SPACE:
			move_vec[1] = 0.2f;
			break;
		case 17:
			move_vec[1] = -0.2f;
			break;
		}

		return 0;
	}

	void Draw() {
		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
		MyBaseWindow::Draw();

		main_viewport_.Draw();

	}

	virtual int Quit(){
		//SDL_Quit();
		//exit(1);
		g_quit = true;
	}

	int ButtonProc () {

		std::cout << "proc called" << std::endl;

		g_render_state.wireframe_mode = !g_render_state.wireframe_mode;

		extern bool view_update_needed;
		view_update_needed = true;

		return 0;
	}
};
